package com.ticket.sass.common.ticketserver.platform.cx300

import com.ticket.sass.common.ticketserver.configure.ConfigData
import com.ticket.sass.common.ticketserver.dto.input.OrderTicketInfoInput
import com.ticket.sass.common.ticketserver.dto.input.OrderTicketSubmitInput
import com.ticket.sass.common.ticketserver.dto.input.SeatLockInput
import com.ticket.sass.common.ticketserver.dto.input.SeatUnlockInput
import com.ticket.sass.common.ticketserver.formatString

object XmlDataManager {
    fun seatLock(configData: ConfigData.CX300, input: SeatLockInput): String {
        val verifyInfo = Configurer.generateTicketSign(
            configData, listOf(input.sessionCode, input.seatList.joinToString("") { it.code }))

        return """<?xml version="1.0"?>
            <LockSeatParameter>
               <AppCode>${configData.appCode}</AppCode>
               <CinemaCode>${configData.cinemaCode}</CinemaCode>
               <FeatureAppNo>${input.sessionCode}</FeatureAppNo>
               <SeatInfos>
                ${input.seatList.joinToString("") { "<SeatCode>${it.code}</SeatCode>" }}
               </SeatInfos>
               <Compress>0</Compress>
               <VerifyInfo>$verifyInfo</VerifyInfo>
           </LockSeatParameter>"""
    }

    fun seatUnlock(configData: ConfigData.CX300, input: SeatUnlockInput): String {
        val verifyInfo = Configurer.generateTicketSign(
            configData, listOf(input.id, input.scheduleCode, input.seatList.joinToString("") { it.code }))

        return """<?xml version="1.0"?>
            <ReleaseSeatParameter>
               <AppCode>${configData.appCode}</AppCode>
               <CinemaCode>${configData.cinemaCode}</CinemaCode>
               <OrderCode>${input.id}</OrderCode>
               <FeatureAppNo>${input.scheduleCode}</FeatureAppNo>
               <SeatInfos>
                ${input.seatList.joinToString("") { "<SeatCode>${it.code}</SeatCode>" }}
               </SeatInfos>
               <Compress>0</Compress>
               <VerifyInfo>$verifyInfo</VerifyInfo>
           </ReleaseSeatParameter>"""
    }

    fun orderTicketSubmit(configData: ConfigData.CX300, input: OrderTicketSubmitInput): String {
        val verifyInfo = Configurer.generateTicketSign(
            configData,
            listOf(
                input.orderNo,
                input.sessionCode,
                input.cellphone,
                input.seatList.joinToString("") {
                    val serviceFee = it.serviceFee + it.handleFee
                    val totalPrice = it.price + serviceFee + it.channelServiceFee

                        // 金额必须保留两位小数
                    "${it.code}${totalPrice.formatString("0.00", 2)}${it.channelServiceFee.formatString("0.00", 2)}${serviceFee}0.00"
                }
            )
        )

        // 金额必须保留两位小数
        return """<?xml version="1.0"?>
            <SubmitOrderParameter>
               <AppCode>${configData.appCode}</AppCode>
               <CinemaCode>${configData.cinemaCode}</CinemaCode>
               <OrderCode>${input.orderNo}</OrderCode>
               <FeatureAppNo>${input.sessionCode}</FeatureAppNo>
               <MobilePhone>${input.cellphone}</MobilePhone>
               <SeatInfos>
                ${input.seatList.joinToString("") {
                    val serviceFee = it.serviceFee + it.handleFee
                    val totalPrice = it.price + serviceFee + it.channelServiceFee
                    """
                        <SeatInfo>
                            <SeatCode>${it.code}</SeatCode>
                            <Price>${totalPrice.formatString("0.00", 2)}</Price>
                            <ServiceCharge>${it.channelServiceFee.formatString("0.00", 2)}</ServiceCharge>
                            <ServiceAddFee>${serviceFee.formatString("0.00", 2)}</ServiceAddFee>
                            <CinemaAllowance>0.00</CinemaAllowance>
                            <MarketingCode></MarketingCode>
                        </SeatInfo>
                    """.trimIndent() 
                }}
               </SeatInfos>
               <Compress>0</Compress>
               <VerifyInfo>$verifyInfo</VerifyInfo>
           </SubmitOrderParameter>"""
    }

    fun orderTicketInfo(configData: ConfigData.CX300, input: OrderTicketInfoInput): String {
        val verifyInfo = Configurer.generateTicketSign(
            configData, listOf(input.code))

        return """<?xml version="1.0"?>
            <QueryTicketInfoParameter>
               <AppCode>${configData.appCode}</AppCode>
               <CinemaCode>${configData.cinemaCode}</CinemaCode>
               <Tickets>
                    <Ticket>
                        <PrintNo>${input.code}</PrintNo>
                    </Ticket>
               </Tickets>
               <Compress>0</Compress>
               <VerifyInfo>$verifyInfo</VerifyInfo>
           </QueryTicketInfoParameter>"""
    }
}